<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>产生一个随机整数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mt-srand.html">« mt_srand</a></li>
      <li style="float: right;"><a href="function.random-bytes.html">random_bytes »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.random.html">Random 函数</a></li>
    <li>产生一个随机整数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.rand" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">rand</h1>
   <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">rand</span> &mdash; <span class="dc-title">产生一个随机整数</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.rand-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>rand</strong></span>(): <span class="type">int</span></div>

  <div class="methodsynopsis dc-description"><span class="methodname"><strong>rand</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$min</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$max</code></span>): <span class="type">int</span></div>

  <p class="simpara">
   如果没有提供可选参数 <code class="parameter">min</code> 和
   <code class="parameter">max</code> 调用 <span class="function"><strong>rand()</strong></span>
   会返回 0 到 <span class="function"><a href="function.getrandmax.html" class="function">getrandmax()</a></span>
   之间的伪随机整数。例如想要 5 到 15（包括 5 和 15）之间的随机数，用
   <code class="literal">rand(5, 15)</code>。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="para">
  本函数并不会生成安全加密的值，并且<em class="emphasis">不可</em>用于加密或者要求返回值不可猜测的目的。
 </p>
 <p class="para">
  如果需要加密安全随机，则可以将 <span class="classname"><a href="class.random-engine-secure.html" class="classname">Random\Engine\Secure</a></span> 引擎用于
  <span class="classname"><a href="class.random-randomizer.html" class="classname">Random\Randomizer</a></span>。对于简单的用例，<span class="function"><a href="function.random-int.html" class="function">random_int()</a></span>
  和 <span class="function"><a href="function.random-bytes.html" class="function">random_bytes()</a></span> 函数提供了操作系统的 <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> 支持的方便且安全的 <abbr title="Application Programming Interface">API</abbr>。
 </p>
</div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    在某些平台下（例如 Windows）<span class="function"><a href="function.getrandmax.html" class="function">getrandmax()</a></span>
    只有 32767。如果需要的范围大于 32767，那么指定 <code class="parameter">min</code>
    和 <code class="parameter">max</code> 参数就可以生成更大的数了，或者考虑用
    <span class="function"><a href="function.mt-rand.html" class="function">mt_rand()</a></span> 来替代之。
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    自 PHP 7.1.0 起，<span class="function"><strong>rand()</strong></span> 和 <span class="function"><a href="function.mt-rand.html" class="function">mt_rand()</a></span>
    使用相同的随机数生成器。为了保持向后兼容性，<span class="function"><strong>rand()</strong></span> 允许
    <code class="parameter">max</code> 小于 <code class="parameter">min</code>，而不是像
    <span class="function"><a href="function.mt-rand.html" class="function">mt_rand()</a></span> 一样，返回 <strong><code>false</code></strong>。
   </span>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.rand-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">min</code></dt>

     <dd>

      <p class="para">
       返回的最低值（默认：0）
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">max</code></dt>

     <dd>

      <p class="para">
       返回的最高值（默认：<span class="function"><a href="function.getrandmax.html" class="function">getrandmax()</a></span>）
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.rand-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   介于 <code class="parameter">min</code> （或是 0）和 <code class="parameter">max</code> （或
   <span class="function"><a href="function.getrandmax.html" class="function">getrandmax()</a></span>，包含该值）之间的伪随机值。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.rand-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><strong>rand()</strong></span> 已收到模偏差的 <a href="migration72.incompatible.html#migration72.incompatible.rand-mt_rand-output" class="link">错误修复</a>。这意味着使用特定种子生成的序列可能与
        64 位机器上的 PHP 7.1.0 不同。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><strong>rand()</strong></span> <a href="migration71.incompatible.html#migration71.incompatible.rand-srand-aliases" class="link">成为</a> <span class="function"><a href="function.mt-rand.html" class="function">mt_rand()</a></span> 的别名。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.rand-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3475">
    <p><strong>示例 #1 <span class="function"><strong>rand()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">15</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
7771
22264
11
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.rand-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    <code class="parameter">min</code> <code class="parameter">max</code> 的范围必须在 <span class="function"><a href="function.getrandmax.html" class="function">getrandmax()</a></span>
    范围内。即 (<code class="parameter">max</code> - <code class="parameter">min</code>) &lt;=
    <span class="function"><a href="function.getrandmax.html" class="function">getrandmax()</a></span>。否则，<span class="function"><strong>rand()</strong></span> 可能会返回质量差的随机数。
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.rand-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.srand.html" class="function" rel="rdfs-seeAlso">srand()</a> - 播下随机数发生器种子</span></li>
    <li class="member"><span class="function"><a href="function.getrandmax.html" class="function" rel="rdfs-seeAlso">getrandmax()</a> - 显示随机数最大的可能值</span></li>
    <li class="member"><span class="function"><a href="function.mt-rand.html" class="function" rel="rdfs-seeAlso">mt_rand()</a> - 通过梅森旋转（Mersenne Twister）随机数生成器生成随机值</span></li>
    <li class="member"><span class="function"><a href="function.random-int.html" class="function" rel="rdfs-seeAlso">random_int()</a> - 生成加密安全的伪随机整数</span></li>
    <li class="member"><span class="function"><a href="function.random-bytes.html" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Get cryptographically secure random bytes</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>